#ifndef RS485_DEV_H
#define RS485_DEV_H

#include <QObject>
#include <QWidget>
#include <QSerialPort>
#include <QSerialPortInfo>

class rs485_dev : public QObject
{
    Q_OBJECT
private:
    bool read_data_ok = false;  //读取数据完成标志
    QByteArray read_buf;
    QSerialPort *serialPort = nullptr;  //串口对象
    qint16 read_data_sz = 0;    //读取到的数据数

public:
    explicit rs485_dev(QString serialName,QObject *parent = nullptr);
    explicit rs485_dev(QSerialPort *serialPort);
    virtual ~rs485_dev();

public:
    bool is_read_data(void){return this->read_data_ok;}
    QByteArray read_data_barray();
    char *read_data_carr();
    qint64 write_data(QByteArray &buf);
    qint64 write_data(const char *buf);
    void read_buf_clear(){this->read_buf.clear();}

    qint8 getRecv_data_bit_len() const;

    static qint8 recv_cnt;
    static qint8 recv_data_bit_len;    //读取数据位数

signals:
    void newValue(QByteArray new_date); //用于多线程时通知其他线程进行数据读取

private slots:
    void serialPortReadyRead();

};

#endif // RS485_DEV_H
